package mt.filter;


import static org.junit.Assert.*;

import java.io.IOException;
import java.net.UnknownHostException;


import mt.Order;
import mt.comm.ClientCommImp;
import mt.comm.ServerCommImp;

import org.junit.Test;

public class AnalyticsFilterTest {
	ServerCommImp server = new ServerCommImp();
	AnalyticsFilter filtroAnalitico = new AnalyticsFilter(server);

	@Test
	public void testEstatistica() throws InterruptedException,
			UnknownHostException, IOException {
		
		// start the filter
		filtroAnalitico.start();

		String erikson = "erikson";
		String jorge = "jorge";

		ClientCommImp cliente_Erikson = new ClientCommImp();
		ClientCommImp cliente_Jorge = new ClientCommImp();

		// connect two clients
		cliente_Erikson.connect(null, erikson);
		filtroAnalitico.getNextMessage();
		cliente_Jorge.connect(null, jorge);
		filtroAnalitico.getNextMessage();

		assertTrue(filtroAnalitico.clientIsConnected(erikson));
		assertTrue(filtroAnalitico.clientIsConnected(jorge));
		


		// automatic order - detected and client remove
		int i = 0;
		while (i++ < 3) {

			cliente_Erikson.sendOrder(Order.createBuyOrder(erikson, jorge, i, 110));
			filtroAnalitico.getNextMessage();

		}

		// the client jorge dosn't violate the term to the automatic order
		i = 0;
		while (i++ < 2) {

			cliente_Jorge.sendOrder(Order.createSellOrder(jorge, erikson, i, 110));
			filtroAnalitico.getNextMessage();

		}

		//i alredy read all the message send?
		assertFalse(filtroAnalitico.hasNextMessage());

		assertFalse(filtroAnalitico.clientIsConnected(erikson));
		assertTrue(filtroAnalitico.clientIsConnected(jorge));

		assertFalse(cliente_Erikson.isConnected());
		assertTrue(cliente_Jorge.isConnected());

		// cliente_Erickson cannot connect

		cliente_Erikson.connect(null, erikson);
		Thread.currentThread().sleep(10000);
		
		// client jorge wasnt disconnect because the 3 order was passed 10 seconds
		cliente_Erikson.sendOrder(Order.createSellOrder(jorge, erikson, i, 110));


		assertTrue(filtroAnalitico.hasNextMessage());
		System.out.println("get");

		filtroAnalitico.getNextMessage();
		
		assertFalse(filtroAnalitico.clientIsConnected(erikson));
		assertFalse(cliente_Erikson.hasNextMessage());
		assertFalse(cliente_Erikson.isConnected());

		assertTrue(filtroAnalitico.clientIsConnected(jorge));

		System.out.print("vou dormir\n");

		Thread.currentThread().sleep(20000);

		cliente_Erikson.connect(null, erikson);
		filtroAnalitico.getNextMessage();

		filtroAnalitico.sendOrder(erikson,Order.createBuyOrder(erikson, "abc", 2, 3));

		filtroAnalitico.sendError(jorge, "test");
		System.out.println("now");
		Thread.currentThread().sleep(200);
		assertTrue(cliente_Erikson.hasNextMessage());
		cliente_Erikson.getNextMessage();
		cliente_Jorge.getNextMessage();
		assertFalse(cliente_Erikson.hasNextMessage());

		assertTrue(filtroAnalitico.clientIsConnected(erikson));
		assertTrue(filtroAnalitico.clientIsConnected(jorge));

		cliente_Erikson.disconnect();

		assertFalse(filtroAnalitico.clientIsConnected(erikson));

		assertFalse(cliente_Erikson.isConnected());

		Thread.currentThread().sleep(90000);

	}

}